﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BookStore.Admin.Models;
using System.Data;
namespace BookStore.Admin.Controllers
{
    [HandleError]
    public class BillController : Controller
    {
        // view login
        public ActionResult index()
        {
            int page = 0;
            int limit = 5;
            string[][] AllBill;
            BillModel bm = new BillModel();
            if (this.Session["Session_Admin"] == null || this.Session["Admin_Type"] == null || Convert.ToInt32(this.Session["Admin_Type"].ToString()) != 1)
            {
                Response.Redirect("/admin/login");
            }
            if ((this.Request.QueryString["page"] != null))
            {
                try
                {
                    page = Convert.ToInt32(this.Request.QueryString["page"]);
                }
                catch (SystemException se)
                {
                    page = 1;
                }
            }
            else
            {
                page = 1;
            }
            string url = "";
            string[] condition = new string[5];
            //------Start Form submit Filter-----//
            if ((this.Request.Form["filter_id"] != null) && (this.Request.Form["filter_id"] != ""))
            {
                condition[0] = this.Request.Form["filter_id"].ToString();
                if (url == "")
                {
                    url += "?";
                }
                else
                {
                    url += "&";
                }
                url += "filter_id=" + condition[0];
                ViewData["filter_id"] = condition[0];
            }
            if ((this.Request.Form["filter_name"] != null) && (this.Request.Form["filter_name"] != ""))
            {
                condition[1] = this.Request.Form["filter_name"].ToString();
                if (url == "")
                {
                    url += "?";
                }
                else
                {
                    url += "&";
                }
                url += "filter_name=" + condition[1];
                ViewData["filter_name"] = condition[1];
            }
            if ((this.Request.Form["filter_status"] != null) && (this.Request.Form["filter_status"] != ""))
            {
                condition[2] = this.Request.Form["filter_status"].ToString();
                if (url == "")
                {
                    url += "?";
                }
                else
                {
                    url += "&";
                }
                url += "filter_status=" + condition[2];
                ViewData["filter_status"] = condition[2];
            }
            if ((this.Request.Form["filter_date"] != null) && (this.Request.Form["filter_date"] != ""))
            {
                string[] time = this.Request.Form["filter_date"].ToString().Split(Convert.ToChar("-"));
                if (time.Length == 3)
                {
                    condition[3] = Utils.DateToInt(this.Request.Form["filter_date"].ToString()).ToString();
                    if (url == "")
                    {
                        url += "?";
                    }
                    else
                    {
                        url += "&";
                    }
                    url += "filter_date=" + condition[3];
                    ViewData["filter_date"] = condition[3];
                }
            }
            if ((this.Request.Form["filter_total"] != null) && (this.Request.Form["filter_total"] != ""))
            {
                condition[4] = this.Request.Form["filter_total"].ToString();
                try
                {
                    Convert.ToInt32(condition[4].ToString());
                }
                catch (SystemException se)
                {
                    Response.Redirect("/admin/bill");
                }
                if (url == "")
                {
                    url += "?";
                }
                else
                {
                    url += "&";
                }
                url += "filter_total=" + condition[4];
                ViewData["filter_total"] = condition[4];
            }
            //------End Form submit Filter-----//

            //------Start Get Filter-----//
            if (url == "")
            {
                if ((this.Request.QueryString["filter_id"] != null) && (this.Request.QueryString["filter_id"] != ""))
                {
                    condition[0] = this.Request.QueryString["filter_id"].ToString();
                    if (url == "")
                    {
                        url += "?";
                    }
                    else
                    {
                        url += "&";
                    }
                    url += "filter_id=" + condition[0];
                    ViewData["filter_id"] = condition[0];
                }
                if ((this.Request.QueryString["filter_name"] != null) && (this.Request.QueryString["filter_name"] != ""))
                {
                    condition[1] = this.Request.QueryString["filter_name"].ToString();
                    if (url == "")
                    {
                        url += "?";
                    }
                    else
                    {
                        url += "&";
                    }
                    url += "filter_name=" + condition[1];
                    ViewData["filter_name"] = condition[1];
                }
                if ((this.Request.QueryString["filter_status"] != null) && (this.Request.QueryString["filter_status"] != ""))
                {
                    condition[2] = this.Request.QueryString["filter_status"].ToString();
                    if (url == "")
                    {
                        url += "?";
                    }
                    else
                    {
                        url += "&";
                    }
                    url += "filter_status=" + condition[2];
                    ViewData["filter_status"] = condition[2];
                }
                if ((this.Request.QueryString["filter_date"] != null) && (this.Request.QueryString["filter_date"] != ""))
                {
                    condition[3] = this.Request.QueryString["filter_date"].ToString();
                    if (url == "")
                    {
                        url += "?";
                    }
                    else
                    {
                        url += "&";
                    }
                    url += "filter_date=" + condition[3];
                    ViewData["filter_date"] = condition[3];

                }
                if ((this.Request.QueryString["filter_total"] != null) && (this.Request.QueryString["filter_total"] != ""))
                {
                    condition[4] = this.Request.QueryString["filter_total"].ToString();
                    try
                    {
                        Convert.ToInt32(condition[4].ToString());
                    }
                    catch (SystemException se)
                    {
                        Response.Redirect("/admin/bill");
                    }
                    if (url == "")
                    {
                        url += "?";
                    }
                    else
                    {
                        url += "&";
                    }

                    url += "filter_total=" + condition[4];
                    ViewData["filter_total"] = condition[4];
                }
            }
            //------End Get Filter-----//
            if (url == "")
            {
                url += "/admin/bill?page=";
            }
            else
            {
                url = "/admin/bill" + url + "&page=";
            }
            AllBill = bm.getAllBillBuy(page, limit, condition);
            ViewData["AllBill"] = AllBill;
            Pagination pagination = new Pagination(url, Convert.ToInt32(bm.countAllBillBuy(condition).ToString()), limit, page);
            ViewData["pagination"] = pagination.render();
            try
            {
                int success = Convert.ToInt32(this.Session["success"]);
                if (success == 1 || success == 2)
                {
                    ViewData["success"] = success;
                    this.Session["success"] = null;
                }
            }
            catch (SystemException se)
            {
                Response.Redirect("/home/index");
            }

            return View();
        }

        public ActionResult delete()
        {
            BillModel Bm = new BillModel();
            BillDetailModel bdm = new BillDetailModel();
            int id = 0;
            int err = 0;
            try
            {
                id = Convert.ToInt32(this.Request.QueryString["bid"].ToString());
            }
            catch (SystemException se)
            {
                this.Session["success"] = 2;
                Response.Redirect("/admin/bill");
                
            }
            err = bdm.deleteBillDetail(id);
            err = Bm.deleteBill(id);
            if (err == -1)
            {
                this.Session["success"] = 2;
                Response.Redirect("/admin/bill");
            }
            this.Session["success"] = 1;
            Response.Redirect("/admin/bill");
          
            return View();
        }

        public ActionResult deletemany()
        {
            BillModel Bm = new BillModel();
            BillDetailModel bdm = new BillDetailModel();
            int id = 0;
            int err = 0;
            if (this.Request.Form["selected[]"] != null)
            {

                foreach (string value in this.Request.Form["selected[]"].Split(','))
                {
                    try
                    {
                        id = Convert.ToInt32(value.ToString());
                    }
                    catch (SystemException se)
                    {
                        Response.Redirect("/admin/bill");
                        this.Session["success"] = 2;
                    }
                    err = bdm.deleteBillDetail(id);
                    err = Bm.deleteBill(id);
                    if (err == -1)
                    {
                        Response.Redirect("/admin/bill");
                        this.Session["success"] = 2;
                    }
                }
                Response.Redirect("/admin/bill");
                this.Session["success"] = 1;
            }
            else
            {
                Response.Redirect("/admin/bill");

            }
            return View();
        }



    }
}
